package com.bidevalution.business.impl;

import com.bidevalution.business.service.BeSectionEnterpriseService;
import com.bidevalution.dao.entity.BeSectionEnterprise;
import com.bidevalution.dao.entity.BeSectionInfo;
import com.bidevalution.dao.entity.BeSectionMethod;
import com.bidevalution.dao.mapper.BeSectionEnterpriseMapper;
import com.bidevalution.dao.mapper.BeSectionInfoMapper;
import com.bidevalution.dao.mapper.BeSectionMethodMapper;
import com.bidevalution.support.context.Constants;
import com.qtp.core.model.Response;
import com.qtp.core.util.RandomUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by glodon on 2019-08-30.
 */
@Service
public class BeSectionEnterpriseServiceImpl implements BeSectionEnterpriseService {
    @Autowired
    private BeSectionEnterpriseMapper beSectionEnterpriseMapper;

    @Autowired
    private BeSectionMethodMapper beSectionMethodMapper;

    @Autowired
    private BeSectionInfoMapper beSectionInfoMapper;

    private static final Integer SECTION_STATUS_BEGIN = 2;
    private static final String IS_REVIEWED_ALERT = "评标已开始";

    /**
     * 根据标段获取投标单位列表
     * @param sectionId
     * @return
     * @throws Exception
     */
    public List<BeSectionEnterprise> getBeSectionEnterpriseList(String sectionId) throws Exception{
        return beSectionEnterpriseMapper.selectBySectionId(sectionId);
    }

    /**
     * 生成暗标号
     * @param sectionId
     * @return
     * @throws Exception
     */
    @Override
    public String generateHideNumber(String sectionId) throws Exception {
        BeSectionMethod beSectionMethod = beSectionMethodMapper.selectByPrimaryKey(sectionId);
        //查询是否是暗标
        if(beSectionMethod != null && !Constants.ZERO.equals(beSectionMethod.getIsBidHide())){
            List<BeSectionEnterprise> beSectionEnterpriseList = beSectionEnterpriseMapper.selectBySectionId(sectionId);
            if(beSectionEnterpriseList != null && beSectionEnterpriseList.size() > 0) {
                for(BeSectionEnterprise beSectionEnterprise : beSectionEnterpriseList) {
                    if(StringUtils.isBlank(beSectionEnterprise.getBidHideNumber())) {
                        String bidHideNumber = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
                        beSectionEnterprise.setBidHideNumber(bidHideNumber);
                    }
                }
                if(beSectionEnterpriseMapper.bathUpdate(beSectionEnterpriseList)>0){
                    return "暗标号生成成功";
                }
            }
        } else {
            return "不需要生成暗标号";
        }
        return "暗标号生成失败";
    }

    /**
     * 查询需要下载并解析的投标单位信息
     * @return
     * @throws Exception
     */
    public List<BeSectionEnterprise> getImportEnterpriseList() throws Exception{
        return beSectionEnterpriseMapper.getImportEnterpriseList();
    }

    @Override
    public List<BeSectionEnterprise> queryEnterBySectionId(String sectionId) throws Exception {
        return beSectionEnterpriseMapper.selectBySectionId(sectionId);
    }

    @Override
    public List<BeSectionEnterprise> getEnterpriseNames(String sectionId) throws Exception {
        return beSectionEnterpriseMapper.getEnterpriseNames(sectionId);
    }
    /**
     * 根据企业的平台id，获取企业信息
     * @param ptEntGuid 企业的平台id
     * @return 返回企业信息
     */
    @Override
    public BeSectionEnterprise getEnterpriseByPtId(String ptEntGuid) {
        return beSectionEnterpriseMapper.getEnterpriseByPtId(ptEntGuid);
    }
	 @Override
     @Transactional(rollbackFor=Exception.class)
    public Response addSectionEnterprise(BeSectionEnterprise item) {
         if(isOpened(item.getSectionId())) {
             return Response.fail(IS_REVIEWED_ALERT);
         }
		 //判断单位名称不能重复
         long num = beSectionEnterpriseMapper.getEnterpriseNum(item.getEnterpriseName(), item.getSectionId());
         if(num>0) {
             return Response.fail("单位名称不能重复");
         }
         item.setBidHideNumber(getHideNumber(item.getSectionId()));
         beSectionEnterpriseMapper.insertSelective(item);
         Map<String, Object> bidInfo = new HashMap<>(8);
         bidInfo.put("biinProjectID", item.getBidProjectid());
         bidInfo.put("biinName", item.getEnterpriseName());
         bidInfo.put("enterpriseId", item.getSeqId());
        return Response.ok(beSectionEnterpriseMapper.insertBidInfo(bidInfo));
    }
    /**
     * 生成4位随机暗标号，并且保证同标段不重复
     * @param sectionId
     * @return
     */
    private String getHideNumber(String sectionId) {
        String hideNum;
        while (true) {
            hideNum = RandomUtil.generateNumString(4);
            BeSectionEnterprise sectionEnterprise = new BeSectionEnterprise();
            sectionEnterprise.setSectionId(sectionId);
            sectionEnterprise.setBidHideNumber(hideNum);
            if(beSectionEnterpriseMapper.getSectionEnterprise(sectionEnterprise) == null) {
                break;
            }
        }
        return hideNum;
    }
    @Override
    @Transactional(rollbackFor=Exception.class)
    public Response delSectionEnterprise(String seqId) {
        BeSectionEnterprise enterprise = beSectionEnterpriseMapper.selectByPrimaryKey(seqId);
        if(enterprise != null) {
            if(isOpened(enterprise.getSectionId())) {
                return Response.fail(IS_REVIEWED_ALERT);
            }
            beSectionEnterpriseMapper.delBidInfo(enterprise.getBidProjectid(), seqId);
        }
        return Response.ok(beSectionEnterpriseMapper.deleteByPrimaryKey(seqId));
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public Response updateEnterpriseName(String sectionId, String projectId, String seqId, String name) {
        if(isOpened(sectionId)) {
            return Response.fail(IS_REVIEWED_ALERT);
        }
        //判断单位名称不能重复
        long num = beSectionEnterpriseMapper.getEnterpriseNum(name, sectionId);
        if(num>0) {
            return Response.fail("单位名称不能重复");
        }
        beSectionEnterpriseMapper.updateBidInfoBiinName(name, projectId, seqId);
        return Response.ok(beSectionEnterpriseMapper.updateEnterpriseName(name, seqId));
    }

    @Override
    public List<BeSectionEnterprise> queryEnterpriseByIdAndPrice(String sectionId) throws Exception {
        return beSectionEnterpriseMapper.queryEnterpriseByIdAndPrice(sectionId);
    }

    @Override
    public int updateScoreBySectionId(String sectionId) throws Exception {
        return beSectionEnterpriseMapper.updateScoreBySectionId(sectionId);
    }

    @Override
    public List<Map<String, Object>> queryDepositInfo(String sectionStepId, String expertId, String flag) throws Exception{
        return beSectionEnterpriseMapper.queryDepositInfo(sectionStepId, expertId, flag);
    }

    @Override
    public void updateDepositTypeById(String enterpriseId, String depositType) throws Exception {
        BeSectionEnterprise beSectionEnterprise = new BeSectionEnterprise();
        beSectionEnterprise.setSeqId(enterpriseId);
        beSectionEnterprise.setPayDepositType(depositType);
        if(beSectionEnterpriseMapper.updateByPrimaryKeySelective(beSectionEnterprise)<Constants.ONE){
            throw new Exception("更新投标单位保证金失败！");
        }
    }

    @Override
    public List<BeSectionEnterprise> queryByCondition(BeSectionEnterprise beSectionEnterprise) throws Exception {
        return beSectionEnterpriseMapper.queryByCondition(beSectionEnterprise);
    }

    private boolean isOpened(String sectionId) {
        BeSectionInfo sectionInfo = beSectionInfoMapper.getSectionInfoById(sectionId);
        return sectionInfo != null && SECTION_STATUS_BEGIN.equals(sectionInfo.getReviewState());
    }
}
